<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<meta name="Author" content="Digital Ways, Val Samko">
<meta name="description" content="Software and web design">
<meta name="keywords" content="software,web,design,program,help">

<script language="JavaScript">
document.write('<link rel="stylesheet" href="http://www.digiways.com/');
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf('opera') != -1) document.write('digiways_opera.css');
else if (agt.indexOf('gecko') != -1) document.write('digiways_mozilla.css');
else if (agt.indexOf('msie') != -1) document.write('digiways_iexplorer.css');
else document.write('digiways_iexplorer.css');
document.write('" type="text/css">');
</script><link rel="stylesheet" href="DigiWays%20-%20%5BAccess%20your%20Outlook%20application%20using%20PHP%5D_files/digiways_mozilla.css" type="text/css">
<noscript>
<LINK REL="stylesheet" TYPE="text/css" HREF="http://www.digiways.com/digiways_iexplorer.css">
</noscript>

<title>DigiWays - [Access your Outlook application using PHP]</title>
</head>
<body topmargin="5" bgcolor="#ffffff" link="#006600">
<center>

<img src="DigiWays%20-%20%5BAccess%20your%20Outlook%20application%20using%20PHP%5D_files/dw.gif" align="middle" border="0" height="60" width="744"><br><table border="0" cellpadding="0" cellspacing="2" height="25" width="748"><tbody><tr><td align="CENTER" width="16%">
<table border="0" cellpadding="1" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="000000">
<table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="#cccccc">
<a href="http://www.digiways.com/" class="TopMenuLink">Home</a></td></tr></tbody></table></td></tr></tbody></table></td><td align="CENTER" width="16%">
<table border="0" cellpadding="1" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="000000">
<table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="#cccccc">
<a href="http://www.digiways.com/design/" class="TopMenuLink">Web design</a></td></tr></tbody></table></td></tr></tbody></table></td><td align="CENTER" width="16%">
<table border="0" cellpadding="1" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="000000">
<table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="#cccccc">
<a href="http://www.digiways.com/software/" class="TopMenuLink">Software</a></td></tr></tbody></table></td></tr></tbody></table></td><td align="CENTER" width="16%">
<table border="0" cellpadding="1" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="000000">
<table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="#006600"><div class="TopMenuLinkActive">Articles</div></td></tr></tbody></table></td></tr></tbody></table></td><td align="CENTER" width="16%">
<table border="0" cellpadding="1" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="000000">
<table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td align="CENTER" bgcolor="#cccccc">
<a href="http://www.digiways.com/sitemap/" class="TopMenuLink">Site Map</a></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" width="744"><tbody><tr><td height="1" width="1"><img alt="" src="DigiWays%20-%20%5BAccess%20your%20Outlook%20application%20using%20PHP%5D_files/td.gif" height="1" width="1"></td><td bgcolor="#ffffff" valign="top" width="748"><table border="0" cellpadding="2" cellspacing="2" width="100%"><tbody><tr><td>					<hr size="2"> &gt; <a href="http://www.digiways.com/articles/">Articles</a> &gt; <a href="http://www.digiways.com/articles/php/">PHP articles</a> &gt; Access your Outlook application using PHP<br><hr size="2"><h2> Access your Outlook application using PHP</h2><p>I have always been surprised 
  from what PHP can do.Using COM objects shows new and best ways for PHP development. 
  The folowing code, which is designed like <i>Step by Step wizard</i>, shows 
  the access to Inbox and Outbox folders in your MS Outlook application.</p>
<p>You should do the folowing:</p>
<p><b><font color="#FF0000">Step1:</font></b> 
  check the system configuration and registry entires.</p>
<p><b>System</b>:<br>
  OS: Windows 98/NT/2000/XP <br>
  MS Outlook<br>PHP &gt;4.0.5</p>
<p><b>Registry:</b></p>
<p>Start regedit.exe from Start 
  Menu-&gt;Run ,check the HKEY_CLASSES_ROOT section for the folowing entires</p>
<p><img src="DigiWays%20-%20%5BAccess%20your%20Outlook%20application%20using%20PHP%5D_files/regedit.gif" height="185" width="402"> 
  <br>
  Outlook.Application and MIME.Session (or MAPI.Session1).If the MAPI.Session 
  is missing you shold do the folowing:</p>
<p><img src="DigiWays%20-%20%5BAccess%20your%20Outlook%20application%20using%20PHP%5D_files/outlook_app.gif" height="70" width="276"> <img src="DigiWays%20-%20%5BAccess%20your%20Outlook%20application%20using%20PHP%5D_files/mapi_session.gif" height="83" width="271"></p>
<p>1.Search your computer for the 
  file named cdo.dll if doesn't exists (cdo.dll is a part from many products like 
  xi-tec <a href="http://phoneoffice.xi-tec.com/">PhoneOffice</a>) you must download 
  it from the <b>Microsoft</b> web site .<br>
  2.Move your cdo.dll in the system32 directory and register it with the regsvr32.exe(just 
  drag the file and drop it over regsvr32.exe file).</p>
<p>After this first step you are 
  ready for the <b><font color="#FF0000">STEP 2</font></b> :Classes</p>
<p>Open your favorite PHP editor 
  open a new file and name it: <b>COutLook.php</b></p>
<p>It is time to write a simple 
  class:</p>
<p><font size="2">&lt;?PHP</font></p>
<p><b><font color="#004080">global</font></b> <font color="#008080">$UnreadMessagesInFolder;</font><br>
  <font color="#004080"><b>class</b></font> <font color="#008080">COutLook</font>{<br>
  function for retreiving messages from the selected folder (Inbox or Outbox)<br>
  <font color="#004080"><b>function</b></font><font color="#008080"> getMessages</font>(<font color="#FF8080">$folder</font>){<br>
</p>
<p>Setup the folder table,.there is 4 elements:</p>
<p>message number,message subject ,message type and date received<br>
</p>
<p><b><font color="#004080">echo</font></b>"<font color="#008080">&lt;body 
  text=darkblue&gt;<br>
  &lt;br&gt;&lt;font color=red face=verdana size=3&gt;&lt;b&gt;$folder&lt;/b&gt;&lt;/font&gt;<br>
  &lt;table width=100%&gt;<br>
  &lt;TR bgcolor=#EEEFFF&gt;&lt;td&gt;&lt;font face=verdana size=2&gt;N:&lt;/td&gt;&lt;td&gt;<br>
  &lt;font face=verdana size=2&gt; Subject&lt;/td&gt;&lt;TD&gt;<br>
  &lt;font face=verdana size=2 &gt;Type&lt;/TD&gt;&lt;TD&gt;&lt;font face=verdana 
  size=2&gt; Date&lt;/TD&gt;&lt;/TR&gt;</font>";</p>
<p>creating the <b>COM</b> instance for Outlook.application and MAPI session(access 
  the outlook folders object)<br>
  <font color="#FF8080">$oOutlook</font> = new <b><font color="#004080">COM</font></b>(<font color="#008080">"Outlook.Application"</font>);<br>
  <font color="#FF8080">$session</font>= new <b><font color="#004080">COM</font></b>(<font color="#008080">"MAPI.Session"</font>);</p>
<p><br>
  Log into the session like default user <br>
  <font color="#FF8080">$session</font><font color="#804040">-&gt;Logon();</font></p>
<p>selecting working folder Inbox ot Outbox/<br>
  <font color="#FF8080">$inb=$session</font><font color="#804040">-&gt;$folder;</font></p>
<p>get the total messages in Folder<br>
  <font color="#FF8080">$messages=$inb</font><font color="#804040">-&gt;Messages-&gt;Count()</font>;</p>
<p>get the elements of the message object</p>
<p><b><font color="#004080">for</font></b>(<font color="#FF8080">$i=</font><font color="#004080">1</font>;<font color="#FF8080">$i</font>&lt;(<font color="#FF8080">$messages</font>+<font color="#004080">1</font>);<font color="#FF8080">$i</font>++){<br>
</p>
<p><font color="#FF8080">$item=$inb</font>-<font color="#804040">&gt;Messages-&gt;item(<font color="#FF8080">$i</font>)</font>;<br>
</p>
<p>date string<br>
  <font color="#FF8080">$timeres=$item</font><font color="#804040">-&gt;TimeReceived();</font><br>
  <font color="#FF8080">$date_vb</font>=<b><font color="#004080">getdate</font></b>(<font color="#FF8080">$timeres</font>);<br>
</p>
<p>date elements<br>
  <font color="#FF8080">$year=$date_vb</font>['year'];<br>
  <font color="#FF8080">$month=$date_vb</font>['mon'];<br>
  <font color="#FF8080">$day=$date_vb</font>['mday'];<br>
</p>
<p>entering the folder elements<br>
  <font color="#004080"><b>echo</b></font> "<font color="#008080">&lt;tr 
  bgcolor=#F0F0F0&gt;&lt;td&gt;&lt;font face=verdana size=2 color=darkblue&gt;$i&lt;/td&gt;&lt;td&gt;&lt;font 
  face=verdana size=2 color=darkblue&gt;<br>
  &lt;a href=view.php?id=$i&amp;folder=$folder target=bottomFrame&gt;&lt;font 
  face=verdana size=2 color=#FF6666&gt;$item-&gt;Subject&lt;/font&gt;&lt;/td&gt;&lt;td&gt;&lt;font 
  face=verdana size=2 color=darkblue&gt;$item-&gt;Type&lt;/td&gt;&lt;td&gt;&lt;font 
  face=verdana size=1 color=darkblue&gt;$year/$month/$day&lt;/td&gt;&lt;/font&gt;&lt;tr&gt;</font>";<br>
  }<br>
  <font color="#004080"><b>echo</b></font>"<font color="#008080">&lt;/table&gt;</font>";<br>
  }</p>
<p>&nbsp;</p>
<p>view mesage from selected folder (Inbox or Outbox) </p>
<p><br>
  <b><font color="#004080">function</font></b> <font color="#008080">ViewMessageFromFolde</font>r(<font color="#FF8080">$id,$folder</font>){<br>
  create new instance of the COM Objects<br>
  <font color="#FF8080">$oOutlook</font> = new <b><font color="#004080">COM</font></b>(<font color="#008080">"Outlook.Application"</font>);<br>
  <font color="#FF8080">$session</font>= new <b><font color="#004080">COM</font></b>(<font color="#008080">"MAPI.Session"</font>);<br>
</p>
<p>Log into the current working session<br>
  <font color="#FF8080">$session</font><font color="#804040">-&gt;Logon();</font></p>
<p>get default folder<br>
  <font color="#FF8080">$inb=$session</font><font color="#804040">-&gt;$folder;</font><br>
</p>
<p><b><font color="#004080">if</font></b>(<font color="#FF8080">$id</font>==""){<br>
  <font color="#004080"><b>echo</b></font> "<font color="#008080">&lt;font 
  face=verdana size=2 color=darkblue&gt;Message Viewer&lt;/font&gt;&lt;br&gt;&lt;font 
  face=verdana size=2 color=red&gt;&lt;center&gt;No Messages Selected&lt;/center&gt;&lt;/font&gt;</font>";<br>
  }<br>
  <font color="#004080"><b>else</b></font>{<br>
  <font color="#FF8080">$idint</font>=(int)<font color="#FF8080">$id</font>;</p>
<p>get the messages in the selested folder<br>
  <font color="#FF8080">$items=$inb</font><font color="#804040">-&gt;Messages-&gt;item(<font color="#FF8080">$idint</font>);</font><br>
</p>
<p>make message status read= true<br>
  <font color="#FF8080">$items</font>-&gt;<font color="#804040">Unread=</font>"<font color="#008080">false</font>";<br>
</p>
<p>Update the message status into Outlook's Inbox<br>
  <font color="#FF8080">$items</font><font color="#804040">-&gt;Update(<b><font color="#004080">true</font></b>);</font><br>
</p>
<p>display the message </p>
<p><font color="#004080"><b>echo</b></font>"<font color="#008080">&lt;font 
  face=verdana size=2 color=darkblue&gt;Message Viewer&lt;/font&gt;</font>";<br>
  <font color="#004080"><b>echo</b></font>"<font color="#008080">&lt;table 
  width=100%&gt;&lt;tr&gt;&lt;td&gt;&lt;font face=verdana size=2 color=darkblue&gt;$i&lt;/td&gt;&lt;td&gt;&lt;font 
  face=verdana size=2 color=darkblue&gt;<br>
  &lt;b&gt;$items-&gt;Subject&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;font face=verdana 
  size=2 color=darkblue&gt;$items-&gt;Type&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/font&gt;&lt;tr&gt;<br>
  &lt;tr&gt;&lt;td colspan=4&gt;&lt;pre&gt;&lt;font face=verdana size=2 
color=darkblue&gt;$items-&gt;Text&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;</font>";<br>
  }<br>
  }</p>
<p> <b><font color="#004080">function</font></b> <font color="#008080">getUnreadinInbox()</font>{</p>
<p><br>
  get unread messages from the Inbox Folder<br>
  <font color="#FF8080">$oOutlook</font> = new <b><font color="#004080">COM</font></b>(<font color="#008080">"Outlook.Application"</font>);<br>
  <font color="#FF8080">$oNs </font>=<font color="#FF8080"> $oOutlook</font><font color="#804040">-&gt;GetNamespace(<font color="#008080">"MAPI"</font>);</font><br>
  <font color="#FF8080">$oFldr</font> = <font color="#FF8080">$oNs-<font color="#804040">&gt;</font></font><font color="#804040">GetDefaultFolder(<b><font color="#004080">olFolderInbox</font></b>);</font><br>
  <font color="#FF8080">$UnreadMessagesInFolder</font> = <font color="#FF8080">$oFldr<font color="#804040">-&gt;</font></font><font color="#804040">UnReadItemCount;</font><br>
  <font color="#004080"><b>return</b> </font><font color="#FF8080">$UnreadMessagesInFolder;</font><br>
  }</p>
<p><b><font color="#004080">function</font></b> <font color="#008080">getUnreadinOutbox()</font>{<br>
  get unread messages from the Outbox Folder<br>
  <font color="#FF8080">$oOutlook</font> = new <b><font color="#004080">COM</font></b>(<font color="#008080">"Outlook.Application"</font>);<br>
  <font color="#FF8080">$oNs </font>=<font color="#FF8080"> $oOutlook</font><font color="#804040">-&gt;GetNamespace(<font color="#008080">"MAPI"</font>);</font><br>
  <font color="#FF8080">$oFldr</font> = <font color="#FF8080">$oNs-<font color="#804040">&gt;</font></font><font color="#804040">GetDefaultFolder(<b><font color="#004080">olFolderOutbox</font></b>);</font><br>
  <font color="#FF8080">$UnreadMessagesInFolder</font> = <font color="#FF8080">$oFldr<font color="#804040">-&gt;</font></font><font color="#804040">UnReadItemCount;</font><br>
  <font color="#004080"><b>return</b> </font><font color="#FF8080">$UnreadMessagesInFolder;</font></p>
<p>}</p>
<p><b><font color="#004080">function</font></b> <font color="#008080">staticFolders()</font>{<br>
  // List of the avaailable folders (static !!!)<br>
  <font color="#FF8080">$unread=$this-&gt;</font><font color="#804040">getUnreadinInbox();</font><br>
  <font color="#FF8080">$out_unr</font>=<font color="#FF8080">$this-&gt;</font><font color="#804040">getUnreadinOutbox();</font><br>
  <font color="#004080"><b>echo</b></font>"<font color="#008080">&lt;font 
  color=blue face = verdana size=1&gt;Available folders in this version are:<br>
  &lt;a href=comunread.php?folder=Inbox&gt;Inbox(&lt;font color=red&gt;$unread&lt;/font&gt;)&lt;/a&gt;<br>
  and &lt;a href=comunread.php?folder=Outbox&gt;Outbox(&lt;font color=red&gt;$out_unr&lt;/font&gt;)&lt;/a&gt;&lt;/font&gt;</font>";<br>
  }</p>
<p><br>
  //end of classs<br>
  }<br>
  ?&gt;<br>
</p>
<p>after this you are ready for the <b><font color="#FF0000">STEP3:</font></b> 
  Implementation:</p>
<p>Make a new file named: comunread.php</p>
<p>and write:</p>
<p>&lt;?PHP<br>
  previous class </p>
<p><b><font color="#004080">require</font></b><font color="#008080">("COutLook.php"</font>);<br>
  make new instance of the class</p>
<p><font color="#FF8080">$class=</font> <b><font color="#004080">new</font></b> 
  <font color="#008080">COutLook</font>;<br>
  <font color="#004080"><b>if </b></font>(<font color="#FF8080">$folder</font>==""){<br>
  <font color="#FF8080">$class-&gt;</font><font color="#804040">staticFolders();</font><br>
  }<br>
  <font color="#004080"><b>else</b></font> {<br>
  <font color="#FF8080">$class-&gt;</font><font color="#804040">staticFolders();</font><br>
  <font color="#FF8080">$class-&gt;</font><font color="#804040">getMessages(<font color="#FF8080">$folder</font>);</font><br>
  }</p>
<p>?&gt;</p>
<p>and new file named view.php</p>
<p>which contains:</p>
<p>&lt;?PHP</p>
<p>previous class <br>
  <b><font color="#004080">require</font></b><font color="#008080">("COutLook.php"</font>);<br>
  <font color="#FF8080">$class=</font> <b><font color="#004080">new</font></b> 
  <font color="#008080">COutLook</font>;</p>
<p>if no messages selected</p>
<p><b><font color="#004080">if</font></b> <font color="#FF8080">($id</font>=="" 
  || <font color="#FF8080">$folder</font>== ""){<br>
  <font color="#004080"><b>echo</b></font> "<font color="#008080">&lt;font 
  face=verdana size=2 color=darkblue&gt;Message Viewer&lt;/font&gt;<br>
  &lt;br&gt;&lt;font face=verdana size=2 color=red&gt;<br>
  &lt;center&gt;No Messages Selected&lt;/center&gt;&lt;/font&gt;</font>";<br>
  }<br>
  <font color="#004080"><b>else</b></font>{</p>
<p>get the message </p>
<p><br>
  <font color="#FF8080">$class-&gt;</font><font color="#804040">ViewMessageFromFolder(<font color="#FF8080">$id,$folder</font>)</font>;<br>
  }<br>
  ?&gt;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>and the make a file named <b>index.php</b> which have 2 frames in main frame 
  you should put the <i>'comunread.php</i>' file and in the bottom frame '<i>view.php</i>'</p>
<p>Then run the index.php!</p>
<p>If you have installed <b>Exchange</b> server you must login in your account 
  when you create the session</p>
<p><font color="#FF8080">$session</font><font color="#804040">-&gt;Logon("your 
  name","your password", true or false for the show the logon dialog);</font></p>
<p><b>Errors:</b></p>
<p><img src="DigiWays%20-%20%5BAccess%20your%20Outlook%20application%20using%20PHP%5D_files/error.gif" height="30" width="329"></p>
<p>If your php returns this error , please check 
  your registry for the COM objects <b>Outlook.application</b> and <b>MAPI.Session,</b><font face="Verdana" size="2">or 
  check your syntax.</font></p>
<br><br>
<hr>
<p align="right">Written by Bogomil Shopov <a href="mailto:admin@purplerain.org">mailto:admin@purplerain.org</a>
<br><br></p></td></tr></tbody></table></td></tr></tbody></table>
</center></body></html>